var pdpPage = avalon.define({
    $id: "pdpPage",
    filter: true,
    type: 0,
    product: {
        "productName": "",
        "partnumber": "",
        "categoryId": "",
        "description": "",
        "status": "",
        "createTime": "",
        "lastUpdate": "",
        "url": "",
        "image": "",
        "specifications": "",
        "partnumber": ""
    },
    datasheet: "",
    email: "",
    content: "",
    validateSendMail: {
        onValidateAll: function(reasons) {
            if (reasons.length) {
                mui.toast(reasons[0].getMessage());
            } else {
                var form = $("#sendMailForm");
                mui.ajax('/mail/send.do', {
                    data: form.serialize(),
                    dataType: 'json',
                    type: 'post',
                    success: function(data) {
                        mui('#emailShare').popover('hide');
                        mui.alert("", data.msg);
                        pdpPage.content = "";
                        pdpPage.email = "";
                    }
                });

            }
        }

    },
    sendMail: function(e) {
        e.preventDefault()
        this.validateSendMail.onManual();
    }
});

mui.ready(function() {

    mui('#scroll').scroll();

    var type = $.getParameter("type");
    if (null != type) {
        pdpPage.type = type;
    }

    var id = $.getParameter("id");
    mui.ajax('/product/detail.json', {
        data: {
            id: id
        },
        dataType: 'json',
        success: function(data) {

            var product = data.product;
            if (typeof(product) != "undefined") {
                pdpPage.product = product;

                if (null != data.datasheet) {
                    pdpPage.datasheet = data.datasheet.url;
                } else {
                    pdpPage.datasheet = "";
                }
                /*var specifications = String(data.product.specifications).replace(/&quot;/g, '"')
                	.replace(/&#39;/g, '\'').replace(/&lt;/g, '<')
                	.replace(/&gt;/g, '>').replace(/&amp;/g, '&');

                var description = String(data.product.description).replace(/&quot;/g, '"')
                	.replace(/&#39;/g, '\'').replace(/&lt;/g, '<')
                	.replace(/&gt;/g, '>').replace(/&amp;/g, '&');*/

                var specifications = String(data.product.specifications).replace(/&amp;/g, '&').replace(/&quot;/g, '"')
                    .replace(/&#39;/g, '\'').replace(/&lt;/g, '<')
                    .replace(/&gt;/g, '>');

                var description = String(data.product.description).replace(/&amp;/g, '&').replace(/&quot;/g, '"')
                    .replace(/&#39;/g, '\'').replace(/&lt;/g, '<')
                    .replace(/&gt;/g, '>');

                $("#specifications").html(specifications);
                $("#description").html(description);
            } else {
                mui.back();
            }

        }
    });

    //收藏
    mui('.header').on('tap', '.collection', function(e) {
        var uid = $("#uid").val();
        if ("" == uid) {
            mui.openWindow({
                url: "dahua://login"
            });
            return false;
        }
        mui.ajax('/product/collect.do', {
            data: {
                sid: id,
                uid: uid
            },
            dataType: 'json',
            success: function(data) {
                mui.alert(data.msg, " ");
            }
        });

    });

    mui(".mui-table-view-cell").on('tap', '.web', function() {
        var url = this.getAttribute('href');
        location.href = "dahua://openweb?" + url;
    });

    mui(".mui-table-view-cell").on('tap', '.datasheet', function() {
        var url = this.getAttribute('href');
        location.href = "dahua://openweb?" + url;
    });

    mui("#whatsappShare").on('tap', '.whatsappSend', function() {
        var product = pdpPage.product;
        var text = "【" + product.productName + " " + product.partnumber + "】  " + product.url + " Tap on the link to open in browser."
        location.href = "dahua://openShare?text=" + text + "&&img=";
    });
    mui(".mui-bar-nav").on('tap', '.mui-icon-white', function() {
        sessionStorage.setItem('todo', 1)
    })
});